package com.chen.authority.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.chen.authority.common.annotation.Log;
import com.chen.authority.common.enums.BusinessType;
import com.chen.authority.common.response.MultiResponse;
import com.chen.authority.common.response.Response;
import com.chen.authority.common.response.SingleResponse;
import com.chen.authority.model.dto.DepartmentDto;
import com.chen.authority.model.qo.DepartmentQo;
import com.chen.authority.model.vo.DepartmentVo;
import com.chen.authority.service.DepartmentService;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 部门管理
 *
 * @author chengy
 * @date 2022/11/19
 */
@Api(tags = "部门管理 DepartmentController")
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/department")
public class DepartmentController {

    private final DepartmentService departmentService;

    @PostMapping
    @ApiOperation("新增部门")
    @SaCheckPermission("system:department:add")
    @Log(module = "系统管理-部门管理", businessType = BusinessType.INSERT)
    public Response add(@Valid @RequestBody DepartmentDto dto) {
        return departmentService.addOrUpdateDepartment(null, dto);
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除部门")
    @SaCheckPermission("system:department:delete")
    @Log(module = "系统管理-部门管理", businessType = BusinessType.DELETE)
    public Response delete(@PathVariable Integer id) {
        return departmentService.deleteDepartmentById(id);
    }

    @PutMapping("/{id}")
    @ApiOperation("更新部门")
    @SaCheckPermission("system:department:update")
    @Log(module = "系统管理-部门管理", businessType = BusinessType.UPDATE)
    public Response update(@PathVariable Integer id, @Valid @RequestBody DepartmentDto dto) {
        return departmentService.addOrUpdateDepartment(id, dto);
    }

    @GetMapping("/{id}")
    @ApiOperation("查询部门")
    @SaCheckPermission("system:department:update")
    public SingleResponse<DepartmentVo> get(@PathVariable Integer id) {
        return SingleResponse.success(departmentService.getById(id).toVo(DepartmentVo.class));
    }

    @GetMapping("/list")
    @ApiOperation("查询部门列表")
    @SaCheckPermission("system:department:list")
    public MultiResponse<DepartmentVo> list(DepartmentQo qo) {
        return MultiResponse.success(departmentService.findDepartmentsByCondition(qo));
    }

}
